﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
    public class TacGia_DAO
    {
        clsDocSQL m_clsDocSQL = new clsDocSQL();
        protected object Getdatarow(DataTable dt, int dong)
        {
            TacGia_DTO tg_dto = new TacGia_DTO();
            tg_dto.Str_MaTacGia = dt.Rows[dong]["MaTacGia"].ToString();
            tg_dto.Str_TenTacGia = dt.Rows[dong]["TenTacGia"].ToString();
            tg_dto.D_NgaySinh = DateTime.Parse(dt.Rows[dong]["NgaySinh"].ToString());
            tg_dto.Str_DiaChi = dt.Rows[dong]["DiaChi"].ToString();
            tg_dto.I_TrangThai = int.Parse(dt.Rows[dong]["TrangThai"].ToString());
            return (object)tg_dto;
        }
        public TacGia_DTO[] DanhSachTacGia()
        {
            DataTable dt = m_clsDocSQL.DocSQLStoreProcedure("sp_LayDanhSachTacGia");
            TacGia_DTO[] arrsach;
            arrsach = new TacGia_DTO[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                object sach = Getdatarow(dt, i);
                arrsach[i] = (TacGia_DTO)sach;
            }
            return arrsach;
        }
        public TacGia_DTO DanhSachTacGia_TheoMaTacGia(String str_MaTacGia)
        {
            SqlParameter[] m_SqlParameter = new SqlParameter[1];
            m_SqlParameter[0] = new SqlParameter("@MaTacGia", str_MaTacGia);
            DataTable dt = m_clsDocSQL.DocSQLStoreProcedure1("sp_LayDanhSachTacGia_TheoMaTacGia", m_SqlParameter);
            TacGia_DTO arr = new TacGia_DTO();
            object tg = Getdatarow(dt, 0);
            arr = (TacGia_DTO)tg;
            return arr;
        }
        public String MaTacGiaTang()
        {
            String kq = "";
            kq = m_clsDocSQL.DocKieuString("Ma_TacGia_Tang");
            return kq.ToString();
        }
        public int ThemTacGia(TacGia_DTO dto)
        {
            SqlParameter[] m_SqlParameter = new SqlParameter[6];
            m_SqlParameter[0] = new SqlParameter("@MaTacGia", dto.Str_MaTacGia);
            m_SqlParameter[1] = new SqlParameter("@TenTacGia", dto.Str_TenTacGia);
            m_SqlParameter[2] = new SqlParameter("@NgaySinh", dto.D_NgaySinh);
            m_SqlParameter[3] = new SqlParameter("@DiaChi", dto.Str_DiaChi);
            m_SqlParameter[4] = new SqlParameter("@TrangThai", dto.I_TrangThai);
            m_SqlParameter[5] = new SqlParameter("@KQ", 0);
            m_SqlParameter[5].Direction = ParameterDirection.Output;
            int KQ = m_clsDocSQL.Insert_Delete_Update("sp_ThemTacGia", m_SqlParameter);
            return KQ;
        }
        public int XoaTacGia(TacGia_DTO dto)//
        {
            SqlParameter[] m_SqlParameter = new SqlParameter[2];
            m_SqlParameter[0] = new SqlParameter("@MaTacGia", dto.Str_MaTacGia);
            m_SqlParameter[1] = new SqlParameter("@KQ", 0);
            m_SqlParameter[1].Direction = ParameterDirection.Output;
            int kq = m_clsDocSQL.Insert_Delete_Update("sp_XoaTacGia", m_SqlParameter);
            return kq;
        }

        public int CapNhatTacGia(TacGia_DTO dto)
        {
            SqlParameter[] m_SqlParameter = new SqlParameter[5];
            m_SqlParameter[0] = new SqlParameter("@MaTacGia", dto.Str_MaTacGia);
            m_SqlParameter[1] = new SqlParameter("@TenTacGia", dto.Str_TenTacGia);
            m_SqlParameter[2] = new SqlParameter("@NgaySinh", dto.D_NgaySinh);
            m_SqlParameter[3] = new SqlParameter("@DiaChi", dto.Str_DiaChi);
            //m_SqlParameter[4] = new SqlParameter("@TrangThai", dto.I_TrangThai);
            m_SqlParameter[4] = new SqlParameter("@KQ", 0);
            m_SqlParameter[4].Direction = ParameterDirection.Output;
            int kq = m_clsDocSQL.Insert_Delete_Update("sp_CapNhatTacGia", m_SqlParameter);
            return kq;
        }
    }
}
